﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using Datagridview;
using System.Reflection;
using DgvFilterPopup;
using TextboxCharInteger;
using System.Text.RegularExpressions;
using System.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using PresentationControls;
using DevExpress.XtraTab;
using DevExpress.XtraEditors.Repository;
using Datagridview;
using Hopthoai;
using DAL;

namespace BLL
{
    public class Until
    {
        public static void HightLightDataGridViewRows(ref DataGridView dgv, DataTable dt)
        {
            // Setting the style of the DataGridView control
            dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point);
            dgv.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ControlDark;
            dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv.DefaultCellStyle.Font = new System.Drawing.Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);
            dgv.DefaultCellStyle.BackColor = Color.Empty;
            dgv.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight;
            dgv.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dgv.GridColor = SystemColors.ControlDarkDark;
            dgv.DataSource = dt;
            // Binding the DataGridViewControl to the DataSet generated above
            // Changing the last column alignment to be in the Right alignment            
            dgv.Columns[dgv.Columns.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            // Adjusting each column to be fit as the content of all its cells, including the header cell
            dgv.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
			HideColumn(dgv);

            //MyDataGridView.Columns[0].Visible = false;
        }
        public static void HightLightDataGridViewRows(ref DataGridView dgv)
        {
            // Setting the style of the DataGridView control
            dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point);
            dgv.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ControlDark;
            dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv.DefaultCellStyle.Font = new System.Drawing.Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);
            dgv.DefaultCellStyle.BackColor = Color.Empty;
            dgv.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight;
            dgv.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dgv.GridColor = SystemColors.ControlDark;

            // Binding the DataGridViewControl to the DataSet generated above
            // Changing the last column alignment to be in the Right alignment            
            dgv.Columns[dgv.Columns.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            // Adjusting each column to fit its cell's content, including the header cell
            dgv.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
			HideColumn(dgv);
            //MyDataGridView.Columns[0].Visible = false;
        }
		public static bool ValidateDATAWithPanel(UserControl f)
		{
			foreach (Control c in f.Controls)
			{
				if (c is Panel)
				{
					foreach (Control m in c.Controls)
					{
						if (m is GroupBox)
						{
							foreach (Control n in m.Controls)
							{
								if (n is TextBox)
								{
									TextBox tb = new TextBox();
									tb = (TextBox)n;
									if (tb.ReadOnly == true)
										continue;
									if ((tb.Text == "" || tb.Text == null) && (tb.Name != "txtfind") && (tb.Name != "txtid"))
										return false;
									else
										return true;
								}
							}
						}

					}
				}
			}
			return false;
		}
		public static bool ValidationData(UserControl f)
		{
			foreach (Control c in f.Controls)
			{
						if (c is GroupBox)
						{
							foreach (Control n in c.Controls)
							{
								if (n is TextBox)
								{
									if (n.Name == "txtid")
										continue;
									if (n.Text == "" || n.Text == null)
										return false;
									else
										return true;
								}
							}
						}
			}
			return false;
		}
        public static void HightLightDataGridViewRow(ref CustomGrid dgv)
        {
            try
            {
                // Setting the style of the DataGridView control
                dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point);
                dgv.ColumnHeadersDefaultCellStyle.BackColor = Color.WhiteSmoke;
                dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
                dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dgv.DefaultCellStyle.Font = new System.Drawing.Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);
                dgv.DefaultCellStyle.BackColor = Color.Empty;
                dgv.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight;
                dgv.CellBorderStyle = DataGridViewCellBorderStyle.Single;
                dgv.GridColor = SystemColors.ControlDarkDark;
                // Changing the last column alignment to be in the Right alignment            
                dgv.Columns[dgv.Columns.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                // Adjusting each column to be fit as the content of all its cells, including the header cell
                dgv.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

                //MyDataGridView.Columns[0].Visible = false;
            }
            catch { }
        }
		public static void HightLightDataGridViewRows(ref DataGridViewRow dgvr)
		{
			// Setting the style of the DataGridViewRow control
			//ColumnHeadersDefaultCellStyle
			dgvr.DataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point);
			dgvr.DataGridView.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ControlDark;
			dgvr.DataGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
			dgvr.DataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
			dgvr.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);
			dgvr.DefaultCellStyle.BackColor = Color.Empty;
			dgvr.DataGridView.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight;
			dgvr.DataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
			dgvr.DataGridView.GridColor = SystemColors.ControlDarkDark;

			// Binding the DataGridViewRowControl to the DataSet generated above
			// Changing the last column alignment to be in the Right alignment            
			dgvr.DataGridView.Columns[dgvr.DataGridView.Columns.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

			// Adjusting each column to be fit as the content of all its cells, including the header cell
			dgvr.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
		}
        public static void InitGrid(ref DataGridView dgv)
        {
            DgvFilterManager dv = new DgvFilterManager();
            dv.DataGridView = (DataGridView)dgv;
        }
		public static void SelectIndex<T>(DataGridViewComboBoxCell cbx, String valueMember,int pos)
		{
				if (cbx.Items.Count > 0)
				{
					T m = (T)cbx.Items[pos];
				
					PropertyInfo prop = m.GetType().GetProperty(valueMember);
					object value = prop.GetValue(m, null);
					cbx.Value = value;
				}
		}
        public static void FormatColor(ref TextBox txt)
        {
            if(txt.Focused)
            {
                txt.BackColor = Color.Yellow;
            }
            else
            {
                txt.BackColor = Color.White;
            }

        }
		public static void FormatColor(ref RichTextBox txt)
		{
			if (txt.Focused)
			{
				txt.BackColor = Color.Yellow;
			}
			else
			{
				txt.BackColor = Color.White;
			}

		}
		public static void FormatColor(ref MaskedTextBox txt)
		{
			if (txt.Focused)
			{
				txt.BackColor = Color.Yellow;
			}
			else
			{
				txt.BackColor = Color.White;
			}

		}
        public static void FormatColor(ref DevExpress.XtraEditors.TextEdit txt)
        {
            if (txt.EditorContainsFocus)
            {
                txt.BackColor = Color.Yellow;
            }
            else
            {
                txt.BackColor = Color.White;
            }
        }
        public static void FormatColor(ref TextboxCharInteger.TextboxCharInteger txt)
        {
            if (txt.Focused)
            {
                txt.BackColor = Color.Yellow;
            }
            else
            {
                txt.BackColor = Color.White;
            }
        }
		public static void ClearWithPanel(UserControl f)
		{
			foreach (Control c in f.Controls)
			{
				if (c is Panel)
				{
					foreach (Control m in c.Controls)
					{
						if (m is GroupBox && m.Name != "groupBox1")
						{
							foreach (Control n in m.Controls)
							{
								if (n is TextBox || n is TextboxCharInteger.TextboxCharInteger || n is DevExpress.XtraEditors.TextEdit)
								{
									if (n is TextboxCharInteger.TextboxCharInteger || n is DevExpress.XtraEditors.TextEdit)
									{
										n.Text = "0";
									}
									else
									{
										n.Text = "";
									}
								}
							}
						}
					}
				}
			}
		}
        public static void Clear(UserControl f)
        {
            foreach (Control c in f.Controls)
            {
                if (c is GroupBox || c is TextboxCharInteger.TextboxCharInteger || c is DevExpress.XtraEditors.TextEdit)
                    foreach (Control d in c.Controls)
                        if (d is TextBox || d is TextboxCharInteger.TextboxCharInteger || d is DevExpress.XtraEditors.TextEdit)
                            d.Text = "";
            }
        }

		public static void LockOrUnlock(UserControl f,String[] data,bool IsLock)
		{
			bool flag = true;

			foreach (Control c in f.Controls)
			{
				if (c is Panel)
				{
					foreach (Control m in c.Controls)
					{
						if (m is GroupBox)
						{
								GroupBox gb = (GroupBox)m;

								foreach (String s in data)
								{
									if (gb.Name == GetControlName(s))
									{
										if (IsLock)
										{
											gb.Enabled = false;
										}
										else
										{
											gb.Enabled = true;
										}
										flag = false;
									}
								}
								if (IsLock && flag)
									gb.Enabled = true;
								if (!IsLock && flag)
									gb.Enabled = false;
								flag = true;
						}
					}
				}
				else
				{
					if (c is GroupBox)
					{
						GroupBox gb = (GroupBox)c;
						foreach (String s in data)
						{
							if (gb.Name == GetControlName(s))
							{
								if (IsLock)
								{
									gb.Enabled = false;
								}
								else
								{
									gb.Enabled = true;
								}
							}
							else
							{
								if (!IsLock)
								{
									gb.Enabled = false;
								}
								else
								{
									gb.Enabled = true;
								}
							}
						}
					}
				}
			}
		}
		public static String GetControlName(string str)
		{
			return "gb" + str;
		}
		public static void ScanTextChangeWithPanelDouble(UserControl f)
		{
			foreach (Control t in f.Controls)
			{
				if (t is Panel)
				{
					foreach (Control m in t.Controls)
					{
						if (m is GroupBox)
						{
							foreach (Control n in m.Controls)
							{ 
								// cac control trong groupbox 
								if (n is TextBox || n is TextboxCharInteger.TextboxCharInteger || n is DevExpress.XtraEditors.TextEdit)
								{
									if (n is TextBox)
									{
										TextBox ts = (TextBox)n;
										if (ts.Focused)
										{

											Until.FormatColor(ref ts);
										}
										else if (ts.ReadOnly)
										{
											continue;
										}
										else
										{
											Until.FormatColor(ref ts);
										}
									}
									else if (n is TextboxCharInteger.TextboxCharInteger)
									{
										TextboxCharInteger.TextboxCharInteger ts = (TextboxCharInteger.TextboxCharInteger)n;
										if (ts.Focused)
										{

											Until.FormatColor(ref ts);
										}
										else if (ts.ReadOnly)
										{
											continue;
										}
										else
										{
											Until.FormatColor(ref ts);
										}
									}
									else if (n is DevExpress.XtraEditors.TextEdit)
									{
										DevExpress.XtraEditors.TextEdit ns = (DevExpress.XtraEditors.TextEdit)n;
										if (ns.EditorContainsFocus)
										{
											Until.FormatColor(ref ns);
										}
										else
										{
											Until.FormatColor(ref ns);
										}
									}
								}
								else if (n is System.Windows.Forms.TabControl)
								{
									foreach (TabPage tb in n.Controls)
									{
										foreach (Control d in tb.Controls)
										{
											if (d is Panel)
											{
												foreach (Control e in d.Controls)
												{
													if (e is TextBox || e is TextboxCharInteger.TextboxCharInteger || e is DevExpress.XtraEditors.TextEdit)
													{
														if (e is TextBox)
														{
															TextBox ts = (TextBox)e;
															if (ts.Focused)
															{

																Until.FormatColor(ref ts);
															}
															else if (ts.ReadOnly)
															{
																continue;
															}
															else
															{
																Until.FormatColor(ref ts);
															}
														}
														else if (e is TextboxCharInteger.TextboxCharInteger)
														{
															TextboxCharInteger.TextboxCharInteger ts = (TextboxCharInteger.TextboxCharInteger)e;
															if (ts.Focused)
															{

																Until.FormatColor(ref ts);
															}
															else if (ts.ReadOnly)
															{
																continue;
															}
															else
															{
																Until.FormatColor(ref ts);
															}
														}
														else if (e is DevExpress.XtraEditors.TextEdit)
														{
															DevExpress.XtraEditors.TextEdit ns = (DevExpress.XtraEditors.TextEdit)e;
															if (ns.EditorContainsFocus)
															{
																Until.FormatColor(ref ns);
															}
															else
															{
																Until.FormatColor(ref ns);
															}
														}
													}
												}
											}
										}

									}
								}
							}
						}
					}
				}
			}
		}
		public static void ScanTextChangeWithPanel(UserControl f)
		{
			foreach (Control t in f.Controls)
			{
				if (t is Panel)
				{
					foreach (Control m in t.Controls)
					{
						if (m is GroupBox)
						{
							foreach (Control d in m.Controls)
							{
								if (d is TextBox)
								{
									TextBox ts = (TextBox)d;
									if (ts.Focused)
									{

										Until.FormatColor(ref ts);
									}
									else if (ts.ReadOnly)
									{
										continue;
									}
									else
									{
										Until.FormatColor(ref ts);
									}
								}
								if (d is TextboxCharInteger.TextboxCharInteger)
								{
									TextboxCharInteger.TextboxCharInteger ts = (TextboxCharInteger.TextboxCharInteger)d;
									if (ts.Focused)
									{

										Until.FormatColor(ref ts);
									}
									else if (ts.ReadOnly)
									{
										continue;
									}
									else
									{
										Until.FormatColor(ref ts);
									}
								}
								if (d is DevExpress.XtraEditors.TextEdit)
								{
									DevExpress.XtraEditors.TextEdit ns = (DevExpress.XtraEditors.TextEdit)d;
									if (ns.EditorContainsFocus)
									{
										Until.FormatColor(ref ns);
									}
									else
									{
										Until.FormatColor(ref ns);
									}
								}
								if (d is MaskedTextBox)
								{
									MaskedTextBox ns = (MaskedTextBox)d;
									if (ns.Focused)
									{
										Until.FormatColor(ref ns);
									}
									else
									{
										Until.FormatColor(ref ns);
									}
								}
							}
						}
					}
				}
			}

		}
        public static void ScanTextChange(UserControl f)
        {
            foreach (Control t in f.Controls)
            {
                if (t is GroupBox)
                {
                    foreach (Control d in t.Controls)
                    {
                        if (d is TextBox)
                        {
                            TextBox ts = (TextBox)d;
                            if (ts.Focused)
                            {

                                Until.FormatColor(ref ts);
                            }
                            else if (ts.ReadOnly)
                            {
                                continue;
                            }
                            else
                            {
                                Until.FormatColor(ref ts);
                            }
                        }
                        if (d is TextboxCharInteger.TextboxCharInteger)
                        {
                            TextboxCharInteger.TextboxCharInteger ts = (TextboxCharInteger.TextboxCharInteger)d;
                            if (ts.Focused)
                            {

                                Until.FormatColor(ref ts);
                            }
                            else if (ts.ReadOnly)
                            {
                                continue;
                            }
                            else
                            {
                                Until.FormatColor(ref ts);
                            }
                        }
                        if (d is DevExpress.XtraEditors.TextEdit)
                        { 
                            DevExpress.XtraEditors.TextEdit ns = (DevExpress.XtraEditors.TextEdit)d;
                            if (ns.EditorContainsFocus)
                            {
                                Until.FormatColor(ref ns);
                            }
                            else
                            {
                                Until.FormatColor(ref ns);
                            }
                        }
                    }
                }
            }

        }
        public static void ScanTextChange(Form f)
        {
            foreach (Control t in f.Controls)
            {
                if (t is GroupBox)
                {
                    foreach (Control d in t.Controls)
                    {
                        if (d is TextBox)
                        {
                            TextBox ts = (TextBox)d;
                            if (ts.Focused)
                            {

                                Until.FormatColor(ref ts);
                            }
                            else if (ts.ReadOnly)
                            {
                                continue;
                            }
                            else
                            {
                                Until.FormatColor(ref ts);
                            }
                        }
                        if (d is TextboxCharInteger.TextboxCharInteger)
                        {
                            TextboxCharInteger.TextboxCharInteger ts = (TextboxCharInteger.TextboxCharInteger)d;
                            if (ts.Focused)
                            {

                                Until.FormatColor(ref ts);
                            }
                            else if (ts.ReadOnly)
                            {
                                continue;
                            }
                            else
                            {
                                Until.FormatColor(ref ts);
                            }
                        }
                    }
                }
            }

        }
		public static void ScanTextChangesNews(Form f)
		{

			foreach (Control c in f.Controls)
			{
				if (c is GroupBox)
				{
					foreach (Control m in c.Controls)
					{
						if (m is TextBox)
						{
							TextBox ts = (TextBox)m;
							if (ts.Focused)
							{

								Until.FormatColor(ref ts);
							}
							else if (ts.ReadOnly)
							{
								continue;
							}
							else
							{
								Until.FormatColor(ref ts);
							}
						}
						if (m is TextboxCharInteger.TextboxCharInteger)
						{
							TextboxCharInteger.TextboxCharInteger ts = (TextboxCharInteger.TextboxCharInteger)m;
							if (ts.Focused)
							{

								Until.FormatColor(ref ts);
							}
							else if (ts.ReadOnly)
							{
								continue;
							}
							else
							{
								Until.FormatColor(ref ts);
							}
						}
					}
				}
			}
		}
        public static void ScanTextChanges(Form f)
        {
            foreach (Control t in f.Controls)
            {
                        if (t is TextBox)
                        {
                            TextBox ts = (TextBox)t;
                            if (ts.Focused)
                            {

                                Until.FormatColor(ref ts);
                            }
                            else if (ts.ReadOnly)
                            {
                                continue;
                            }
                            else
                            {
                                Until.FormatColor(ref ts);
                            }
                        }
                        if (t is TextboxCharInteger.TextboxCharInteger)
                        {
                            TextboxCharInteger.TextboxCharInteger ts = (TextboxCharInteger.TextboxCharInteger)t;
                            if (ts.Focused)
                            {

                                Until.FormatColor(ref ts);
                            }
                            else if (ts.ReadOnly)
                            {
                                continue;
                            }
                            else
                            {
                                Until.FormatColor(ref ts);
                            }
                        }
            }
        }
        public static void ScanNullText(UserControl f)
        {
            foreach (Control t in f.Controls)
            {
                if (t is GroupBox)
                {
                    foreach (Control d in t.Controls)
                    {
                        if (d is TextBox)
                        {
                            //kiem tra null 
                            TextBox vb = (TextBox)d;
                            if (vb.Text == "" || vb.Text == null)
                            {
                                frmHoidap noi = new frmHoidap("Không thể để trống giá trị", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                noi.ShowDialog();
                                vb.Focus();
                                return;
                            }
                        }
                        if (d is TextboxCharInteger.TextboxCharInteger)
                        {
                            TextboxCharInteger.TextboxCharInteger ts = (TextboxCharInteger.TextboxCharInteger)d;
                            if (ts.Text == "" || ts.Text == null)
                            {
                                frmHoidap noi = new frmHoidap("Không thể để trống giá trị", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                noi.ShowDialog();
                                ts.Focus();
                                return;
                            }
                        }


                    }
                }
            }
        }
        public static bool CheckNull(TextBox t)
        {
            if (t.Text == "" || t.Text == null)
                return true;
            return false;
        }
        public static void CheckNulls(TextBox t)
        {
            if (t.Text == "" || t.Text == null)
            {
                frmHoidap noi = new frmHoidap("Không thể để trống giá trị", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                noi.ShowDialog();
                t.Focus();
            }
        }
        public static bool CheckNull(TextboxCharInteger.TextboxCharInteger t)
        {
            if (t.Text == "" || t.Text == null)
                return true;
            return false;
        }
        public static void CheckEmail(TextBox t)
        {
            Regex re = new Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (re.IsMatch(t.Text))
            {
                return;
            }
            else
            {
                frmHoidap noi = new frmHoidap("Địa chỉ email sai định kiểu!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                noi.ShowDialog();
                t.Focus();
            }
        }
		/// <summary>
		/// Disable only a specified text box
		/// </summary>
		/// <param name="o">o is a text box object</param>
		public static void LockTextBox(object o)
		{
			if (o is TextBox)
			{
				((TextBox)o).Enabled = false;
				((TextBox)o).ReadOnly = true;
				((TextBox)o).BackColor = SystemColors.Control;
			}
			if (o is MaskedTextBox)
			{
				((MaskedTextBox)o).Enabled = false;
				((MaskedTextBox)o).ReadOnly = true;
				((MaskedTextBox)o).BackColor = SystemColors.Control;
			}
			if (o is DateTimePicker)
			{
				((DateTimePicker)o).Enabled = false;
			}
			if (o is System.Windows.Forms.ComboBox)
			{
				((System.Windows.Forms.ComboBox)o).Enabled = false;
			}
		}
		/// <summary>
		/// Disable all text boxes inside a container object (user control, form, ...)
		/// </summary>
		/// <param name="o">o is a parent/container object (user control, form, ...)</param>
		public static void LockTextBoxes(object o)
		{
			if (o is Control)
			{
				foreach (Control c in ((Control)o).Controls)
				{
					if (c is GroupBox)
					{
						foreach (Control d in c.Controls)
						{
							if (d is TextBox && d.Name.IndexOf("ma") != -1)
							{
								d.Enabled = false;
								((TextBox)d).ReadOnly = true;
								d.BackColor = SystemColors.Control;
							}
						}
					}
				}
			}
		}
		/// <summary>
		/// Enable only a specified text box
		/// </summary>
		/// <param name="o">o is a text box object</param>
		public static void UnlockTextBox(object o)
		{
			if (o is TextBox || o is MaskedTextBox)
			{
				((TextBox)o).Enabled = true;
				((TextBox)o).ReadOnly = false;
				((TextBox)o).BackColor = SystemColors.Window;
			}
			if (o is MaskedTextBox)
			{
				((MaskedTextBox)o).Enabled = true;
				((MaskedTextBox)o).ReadOnly = false;
				((MaskedTextBox)o).BackColor = SystemColors.Control;
			}
			if (o is DateTimePicker)
			{
				((DateTimePicker)o).Enabled = true;
			}
			if (o is System.Windows.Forms.ComboBox)
			{
				((System.Windows.Forms.ComboBox)o).Enabled = false;
			}
		}
		/// <summary>
		/// Enable all text boxes inside a container object (user control, form, ...)
		/// </summary>
		/// <param name="o">o is a parent/container object (user control, form, ...)</param>
		public static void UnlockTextBoxes(object o)
		{
			if (o is Control)
			{
				foreach (Control c in ((Control)o).Controls)
				{
					if (c is GroupBox)
					{
						foreach (Control d in c.Controls)
						{
							if (d is TextBox)
							{
								d.Enabled = true;
								((TextBox)d).ReadOnly = false;
								d.BackColor = SystemColors.Window;
							}
						}
					}
				}
			}
		}
		public static void LoadDatagridviewComboboxCell<T>(ref DataGridView dgv, IList<T> list, String DisplayColumn, String valueColumn, int pos)
		{
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[pos] as DataGridViewComboBoxCell).DataSource=null;
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[pos] as DataGridViewComboBoxCell).Items.Clear();
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[pos] as DataGridViewComboBoxCell).DataSource = list;
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[pos] as DataGridViewComboBoxCell).DisplayMember = DisplayColumn;
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[pos] as DataGridViewComboBoxCell).ValueMember = valueColumn;
		}
		public static void LoadDatagridviewComboboxCell<T>(ref DataGridView dgv, IList<T> list, String DisplayColumn, String valueColumn)
		{
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[GetCurrentCellIndex(dgv)] as DataGridViewComboBoxCell).Items.Clear();
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[GetCurrentCellIndex(dgv)] as DataGridViewComboBoxCell).DataSource = list;
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[GetCurrentCellIndex(dgv)] as DataGridViewComboBoxCell).DisplayMember = DisplayColumn;
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[GetCurrentCellIndex(dgv)] as DataGridViewComboBoxCell).ValueMember = valueColumn;
			(dgv.Rows[GetCurrentRowIndex(dgv)].Cells[GetCurrentCellIndex(dgv)] as DataGridViewComboBoxCell).ReadOnly = false;
		}

		//Get row and cell index
		public static int GetCurrentRowIndex(DataGridView dgv)
		{
			return dgv.CurrentRow.Index;
		}
		public static int GetCurrentCellIndex(DataGridView dgv)
		{
			return dgv.CurrentCell.ColumnIndex + 1;
		}

		public static string FindNameColumn(DataGridView dgv)
		{
			DataGridViewCell cell = dgv.CurrentCell;
			return dgv.Columns[cell.ColumnIndex].Name;
		}
        public static void AddThousandToDecimalTextbox(ref TextBox txt)
        {
           txt.Text.Replace(@"/(\d)(?=(\d\d\d)+([^\d]|$))/g", "1,");
        }
        public static void AddThousandToDecimalTextbox(ref TextboxCharInteger.TextboxCharInteger txt)
        {
            txt.Text.Replace(@"/(\d)(?=(\d\d\d)+([^\d]|$))/g", "1,");
        }

		public static System.Boolean IsNumeric(System.Object Expression)
		{
			if (Expression == null || Expression is DateTime)
				return false;
			if (Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
				return true;
			try
			{
				if (Expression is string)
					Double.Parse(Expression as string);
				else
					Double.Parse(Expression.ToString());
				return true;
			}
			catch { }
			return false;
		}
        public static int FindItemIndex<T>(MultiColumnCombobox.MultiColumnComboBox mt, String NameFind, long value)
        {
            int pos = 0;
            T newst = default(T);
            foreach (object obj in mt.Items)
            {
                newst = Activator.CreateInstance<T>();
                newst = (T)obj;
                PropertyInfo pr = newst.GetType().GetProperty(NameFind);
                Object giatri = pr.GetValue(obj, null);
                if (long.Parse(giatri.ToString()) == value)
                {
                    return pos;
                }
                pos++;
            }
            return pos;
        }
		public static int FindItemIndex<T>(System.Windows.Forms.ListBox mt, String NameFind, long value)
		{
			int pos = 0;
			T newst = default(T);
			foreach (object obj in mt.Items)
			{
				newst = Activator.CreateInstance<T>();
				newst = (T)obj;
				PropertyInfo pr = newst.GetType().GetProperty(NameFind);
				Object giatri = pr.GetValue(obj, null);
				if (long.Parse(giatri.ToString()) == value)
				{
					return pos;
				}
				pos++;
			}
			return pos;
		}
		public static int FindItemIndex<T>(System.Windows.Forms.ComboBox mt, String NameFind, long value)
		{
			int pos = 0;
			T newst = default(T);
			foreach (object obj in mt.Items)
			{
				newst = Activator.CreateInstance<T>();
				newst = (T)obj;
				PropertyInfo pr = newst.GetType().GetProperty(NameFind);
				Object giatri = pr.GetValue(obj, null);
				if (long.Parse(giatri.ToString()) == value)
				{
					return pos;
				}
				pos++;
			}
			if (pos == mt.Items.Count)
				pos = 0;
			return pos;
		}
        public static T ConvertDataRowView<T>(MultiColumnCombobox.MultiColumnComboBox mt)
        {
            DataRowView dr = (DataRowView)mt.SelectedItem;
            return CreateItem<T>(dr.Row);
        }
		
        public static T CreateItem<T>(DataRow row)
        {
            T obj = default(T);
            if (row != null)
            {
                obj = Activator.CreateInstance<T>();

                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                    try
                    {
                        object value = row[column.ColumnName];
                        prop.SetValue(obj, value, null);
                    }
                    catch (ArgumentException ex)
                    {
                        continue;
                    }
                    catch { throw; }
                }
            }

            return obj;
        }
        public static void DisableALLControl(UserControl f)
        {
            foreach (Control c in f.Controls)
            {
                if (c is UserControl)
                {
                    continue;
                }
                c.Enabled = false;
            }
        }
		private delegate void dDisable(UserControl f);
		public static void DisableALLControls(UserControl f)
		{
			if (f.InvokeRequired)
			{
				f.Invoke(new dDisable(DisableALLControls),f);
			}
			else
			{
				foreach (Control c in f.Controls)
				{
					if (c is UserControl)
					{
						continue;
					}
					if (c.Name == "groupBox3")
					{
						continue;
					}
					if (c is GroupBox)
					{
						foreach (Control m in c.Controls)
						{
							if (m is XtraTabControl)
							{
								continue;
							}
							else
							{
								m.Enabled = false;
							}
						}

					}

				}
			}
		}
		public static void EnableALLControls(UserControl f)
		{

			foreach (Control c in f.Controls)
			{
				if (c is UserControl)
				{
					continue;
				}
				if (c.Name == "groupBox3")
				{
					c.Enabled = true;
				}
				if (c is GroupBox)
				{
					if (c.Name != "groupBox3")
					{
						foreach (Control m in c.Controls)
						{
							if (m is XtraTabControl)
							{
								m.Enabled = true;
							}
							else
							{
								m.Enabled = false;
							}
						}
					}

				}

			}
		}
        public static void EnableALLControl(UserControl f)
        {
            foreach (Control c in f.Controls)
            {
                if (c is TextBox)
                {
                    TextBox te = (TextBox)c;
                    if (te.ReadOnly)
                    {
                        te.Enabled = false;
                    }
                }
                c.Enabled = true;
            }
        }

		public static ListSelectionWrapper<T> ConvertType<T>(List<T> ts,String NAME)
		{
			ListSelectionWrapper<T> danhsach = new ListSelectionWrapper<T>(ts,NAME);
			return danhsach;
		}
		public static void EditCheckItem<T>(ref CheckedComboBoxEdit cmb,List<T> dich,String NameFind)
		{ 
			T newst = default(T);

			foreach(CheckedListBoxItem  c in cmb.Properties.Items)
			{
				foreach (Object obj in dich)
				{
					newst = Activator.CreateInstance<T>();
					newst = (T)obj;
					PropertyInfo pr = newst.GetType().GetProperty(NameFind);
					try
					{
						Object giatri = pr.GetValue(obj, null);
					if (c.Value.ToString() == giatri.ToString())
					{
						c.CheckState = CheckState.Checked;
					}
					}
					catch { return; }
				}
			}
		}
		public static DataGridViewRow ConvertObjectToDataGridviewRow<T>(T obj)
		{
			obj = default(T);
			obj = Activator.CreateInstance<T>();
			DataGridViewRow dr = new DataGridViewRow();
			foreach (PropertyInfo pr in obj.GetType().GetProperties())
			{
				try
				{
					DataGridViewTextBoxCell tx = new DataGridViewTextBoxCell();
					PropertyInfo prop = obj.GetType().GetProperty(pr.Name);
					tx.Value = prop.GetValue(obj, null);
					dr.Cells.Add(tx);
				}
				catch (ArgumentException ex)
				{
					continue;
				}
				catch (NullReferenceException ex2)
				{
					continue;
				}
				catch { return dr; }
			}
			return dr;
		}
	
		public static T ConvertDataGridViewRowToObject<T>(DataGridViewRow row)
		{
			T obj = default(T);
			obj = Activator.CreateInstance<T>();
			foreach (PropertyInfo pr in obj.GetType().GetProperties())
			{
				try
				{
					if (pr.Name == "STT")
						continue;
					else
					{
						Int64 a; Int32 b; Boolean c; Decimal d; DateTime e;
						PropertyInfo prop = obj.GetType().GetProperty(pr.Name);

						if (row.Cells[pr.Name].Value != null)
						{
							object value = row.Cells[pr.Name].Value;
							if (value is Int64)
							{
								Int64.TryParse(value.ToString(), out a);
								prop.SetValue(obj, a, null);
								continue;
							}
							if (value is Int32)
							{
								Int32.TryParse(value.ToString(), out b);
								prop.SetValue(obj, b, null);
								continue;
							}
							if (value is Boolean)
							{
								bool.TryParse(value.ToString(), out c);
								prop.SetValue(obj, c, null);
								continue;
							}
							if (value is Decimal)
							{
								decimal.TryParse(value.ToString(), out d);
								prop.SetValue(obj, d, null);
								continue;
							}
							if (value is DateTime)
							{
								DateTime.TryParse(value.ToString(), out e);
								prop.SetValue(obj, e, null);
								continue;
							}
							else    //la kieu string thi gan thang value vao, dung chung cho moi kieu khac
							{
								prop.SetValue(obj, value, null);
								continue;
							}
						}
						else //neu value null thi tao mot object moi gan vao
						{
							prop.SetValue(obj, new object(), null);
						}
					}
				}
				catch (ArgumentException)
				{
					continue;
				}
				catch (NullReferenceException)
				{
					continue;
				}
				catch { return default(T); }
			}
			return obj;
		}
		private static string getControlNames(bool flag, string str)
		{
			if (flag)
				return "gb" + str;
			else
				return "txt_" + str;
		}

		public static void SetDefaultMoney(UserControl uc, string[] n)	//only use for special money fields: txt_DG, txt_TT
		{
			foreach (Control c in uc.Controls)
			{
				if (c is Panel)
				{
					foreach (Control d in c.Controls)
					{
						if (d is GroupBox)
						{
							foreach (Control e in d.Controls)
							{
								foreach (string s in n)
								{
									if (e is TextBox)
									{
										if (e.Name == getControlNames(false, s))
										{
											e.Text = "0";
										}
									}
									if (e is DevExpress.XtraEditors.TextEdit)
									{
										if (e.Name == getControlNames(false, s))
										{
											e.Text = "0";
										}
									}
								}
							}
						}
					}
				}
			}
		}

		public static void HideColumns(DataGridView dgv, string[] colname)
		{
			foreach (DataGridViewColumn col in dgv.Columns)
			{
				for (int i = 0; i < colname.Length; i++)
				{
					if (col.Name == colname[i])
					{
						col.Visible = false;
						break;
					}
					else
					{
						col.Visible = true;
					}
				}
			}
		}
		public static void HideColumn(DataGridView dgv)
		{
			foreach (DataGridViewColumn col in dgv.Columns)
			{
				if ((col.Name.IndexOf("ID") !=-1) || (col.Name.IndexOf("STATUS") !=-1))
				{
					col.Visible = false;
				}
				else
				{
					col.Visible = true;
				}
			}
		}
/// <summary>
		/// Clear all text fields' content (TextBox and TextEdit) in a GroupBox, except those controls specified.<para />
		/// If there is no exception, just declare a new empty string array.
		/// </summary>
		/// <param name="g">The GroupBox control to be used</param>
		/// <param name="except">A string array contains name of do-not-clear controls</param>
		public static void ClearAll(GroupBox g, string[] except)
		{
			int n = 0;
			if (except != null)
				n = except.Length;

			foreach (Control c in g.Controls)
			{
				if (c is GroupBox)
					ClearAll((GroupBox)c, except);
				else  //c is other controls
				{
					if (c is TextBox || c is DevExpress.XtraEditors.TextEdit || c is CheckBox)
					{
						if (n > 0)
						{
							for (int i = 0; i < n; i++)
							{
								if (except[i] == c.Name)
									break;
								else
								{
									if (i == n - 1)
									{
										if (c is TextBox || c is DevExpress.XtraEditors.TextEdit)
											c.Text = "";
										else
											((CheckBox)c).Checked = false;
									}
								}
							}
						}
						else
						{
							if (c is TextBox || c is DevExpress.XtraEditors.TextEdit)
							{
								c.Text = "";
							}
							else
							{
								((CheckBox)c).Checked = false;
							}
						}
					}
				}
			}
		}

		public static void hideControls(GroupBox g, string[] con)
		{
			int n = 0;
			if (con != null)
				n = con.Length;

			foreach (Control c in g.Controls)
			{
				if (n > 0)
				{
					for (int i = 0; i < con.Length; i++)
					{
						if (c.Name == con[i])
							c.Visible = false;
						else
							continue;
					}
				}
				else
				{
					c.Visible = false;
				}
			}
		}

		public static void showControls(GroupBox g, string[] con)
		{
			int n = 0;
			if (con != null)
				n = con.Length;

			foreach (Control c in g.Controls)
			{
				if (n > 0)
				{
					for (int i = 0; i < con.Length; i++)
					{
						if (c.Name == con[i])
							c.Visible = true;
						else
							continue;
					}
				}
				else
				{
					c.Visible = true;
				}
			}
		}


		private static string Chu(string gNumber)
		{
			string result = "";
			switch (gNumber)
			{
				case "0":
					result = "không";
					break;
				case "1":
					result = "một";
					break;
				case "2":
					result = "hai";
					break;
				case "3":
					result = "ba";
					break;
				case "4":
					result = "bốn";
					break;
				case "5":
					result = "năm";
					break;
				case "6":
					result = "sáu";
					break;
				case "7":
					result = "bảy";
					break;
				case "8":
					result = "tám";
					break;
				case "9":
					result = "chín";
					break;
			}
			return result;
		}
		private static string Donvi(string so)
		{
			string Kdonvi = "";

			if (so.Equals("1"))
				Kdonvi = "";
			if (so.Equals("2"))
				Kdonvi = "nghìn";
			if (so.Equals("3"))
				Kdonvi = "triệu";
			if (so.Equals("4"))
				Kdonvi = "tỉ";
			if (so.Equals("5"))
				Kdonvi = "nghìn tỉ";
			if (so.Equals("6"))
				Kdonvi = "triệu ti";
			if (so.Equals("7"))
				Kdonvi = "tỉ tỉ";
			return Kdonvi;
		}
		private static string Tach(string tach3)
		{
			string Ktach = "";
			if (tach3.Equals("000"))
				return "";
			if (tach3.Length == 3)
			{
				string tr = tach3.Trim().Substring(0, 1).ToString().Trim();
				string ch = tach3.Trim().Substring(1, 1).ToString().Trim();
				string dv = tach3.Trim().Substring(2, 1).ToString().Trim();

				if (tr.Equals("0") && ch.Equals("0"))
					Ktach = " không trăm lẻ " + Chu(dv.ToString().Trim()) + " ";
				if (!tr.Equals("0") && ch.Equals("0") && dv.Equals("0"))
					Ktach = Chu(tr.ToString().Trim()).Trim() + " trăm ";
				if (!tr.Equals("0") && ch.Equals("0") && !dv.Equals("0"))
					Ktach = Chu(tr.ToString().Trim()).Trim() + " trăm lẻ " + Chu(dv.Trim()).Trim() + " ";
				if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
					Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mười " + Chu(dv.Trim()).Trim() + " ";
				if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && dv.Equals("0"))
					Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mười ";
				if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && dv.Equals("5"))
					Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mười lăm ";
				if (tr.Equals("0") && ch.Equals("1") && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
					Ktach = " không trăm mười " + Chu(dv.Trim()).Trim() + " ";
				if (tr.Equals("0") && ch.Equals("1") && dv.Equals("0"))
					Ktach = " không trăm mười ";
				if (tr.Equals("0") && ch.Equals("1") && dv.Equals("5"))
					Ktach = " không trăm mười lăm ";
				if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
					Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mười " + Chu(dv.Trim()).Trim() + " ";
				if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && dv.Equals("0"))
					Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mười ";
				if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && dv.Equals("5"))
					Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mười lăm ";
				if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
					Ktach = Chu(tr.Trim()).Trim() + " trăm mười " + Chu(dv.Trim()).Trim() + " ";

				if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && dv.Equals("0"))
					Ktach = Chu(tr.Trim()).Trim() + " trăm mười ";
				if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && dv.Equals("5"))
					Ktach = Chu(tr.Trim()).Trim() + " trăm mười lăm ";
			}



			return Ktach;

		}
		public static string So_chu(double gNum)
		{
			if (gNum == 0)
				return "Không đúng";
			string lso_chu = "";
			string tach_mod = "";
			string tach_conlai = "";
			double Num = Math.Round(gNum, 0);
			string gN = Convert.ToString(Num);
			int m = Convert.ToInt32(gN.Length / 3);
			int mod = gN.Length - m * 3;
			string dau = "[+]";
			// Dau [+ , - ]
			if (gNum < 0)
				dau = "[-]";
			dau = "";
			// Tach hang lon nhat
			if (mod.Equals(1))
				tach_mod = "00" + Convert.ToString(Num.ToString().Trim().Substring(0, 1)).Trim();
			if (mod.Equals(2))
				tach_mod = "0" + Convert.ToString(Num.ToString().Trim().Substring(0, 2)).Trim();
			if (mod.Equals(0))
				tach_mod = "000";
			// Tach hang con lai sau mod
			if (Num.ToString().Length > 2)
				tach_conlai = Convert.ToString(Num.ToString().Trim().Substring(mod, Num.ToString().Length - mod)).Trim();

			///don vi hang mod 
			int im = m + 1;
			if (mod > 0)
				lso_chu = Tach(tach_mod).ToString().Trim() + " " + Donvi(im.ToString().Trim());
			/// Tach 3 trong tach_conlai

			int i = m;
			int _m = m;
			int j = 1;
			string tach3 = "";
			string tach3_ = "";

			while (i > 0)
			{
				tach3 = tach_conlai.Trim().Substring(0, 3).Trim();
				tach3_ = tach3;
				lso_chu = lso_chu.Trim() + " " + Tach(tach3.Trim()).Trim();
				m = _m + 1 - j;
				if (!tach3_.Equals("000"))
					lso_chu = lso_chu.Trim() + " " + Donvi(m.ToString().Trim()).Trim();
				tach_conlai = tach_conlai.Trim().Substring(3, tach_conlai.Trim().Length - 3);

				i = i - 1;
				j = j + 1;
			}
			if (lso_chu.Trim().Substring(0, 1).Equals("k"))
				lso_chu = lso_chu.Trim().Substring(10, lso_chu.Trim().Length - 10).Trim();
			if (lso_chu.Trim().Substring(0, 1).Equals("l"))
				lso_chu = lso_chu.Trim().Substring(2, lso_chu.Trim().Length - 2).Trim();
			if (lso_chu.Trim().Length > 0)
				lso_chu = dau.Trim() + " " + lso_chu.Trim().Substring(0, 1).Trim().ToUpper() + lso_chu.Trim().Substring(1, lso_chu.Trim().Length - 1).Trim() + " đồng chẵn.";

			return lso_chu.ToString().Trim();
		}

    }
}